import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _assertThisInitialized from "@babel/runtime/helpers/esm/assertThisInitialized";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _find from "lodash/find";
import _isString from "lodash/isString";
import _forEach from "lodash/forEach";
import _isBoolean from "lodash/isBoolean";
import _isFunction from "lodash/isFunction";

/**
 * index.js
 * @date 2018-10-02
 */
import React from 'react';
import FieldProp from "./FieldProp";
import ComponentProp from "./ComponentProp";
import { attributeNameProp, attributeTypeProp, attributeValueProp, fieldLabelProp, fieldNameProp } from "../../config";
import DataType from "../../DataType";
import fieldComponents from "../FiledComponent/index";

var DynamicForm = /*#__PURE__*/function (_React$Component) {
  _inherits(DynamicForm, _React$Component);

  var _super = _createSuper(DynamicForm);

  function DynamicForm(props) {
    var _this;

    _classCallCheck(this, DynamicForm);

    _this = _super.call(this, props);
    _this.handleFieldValuesChange = _this.handleFieldValuesChange.bind(_assertThisInitialized(_this));
    _this.handleComponentValuesChange = _this.handleComponentValuesChange.bind(_assertThisInitialized(_this)); // 拿到 form 的ref

    _this.editFormRef = React.createRef();
    return _this;
  }

  _createClass(DynamicForm, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          field = _this$props.field,
          component = _this$props.component;

      if (field) {
        // edit field
        return /*#__PURE__*/React.createElement(FieldProp, Object.assign({}, this.props, {
          onValuesChange: this.handleFieldValuesChange,
          ref: this.editFormRef
        }));
      } else if (component) {
        // edit component
        return /*#__PURE__*/React.createElement(ComponentProp, Object.assign({}, this.props, {
          onValuesChange: this.handleComponentValuesChange,
          ref: this.editFormRef
        }));
      } else {
        // no edit component or field
        return null;
      }
    }
    /**
     * deal field's attribute change
     */

  }, {
    key: "handleFieldValuesChange",
    value: function handleFieldValuesChange(props, changeValues, allValues) {
      var _this2 = this;

      // 使用 allValues 重写 field 的属性
      var _this$props2 = this.props,
          field = _this$props2.field,
          component = _this$props2.component,
          onRefresh = _this$props2.onRefresh;

      var _ref = this.editFormRef.current || {},
          validateFields = _ref.validateFields;

      if (validateFields) {
        validateFields(function (err) {
          if (!err) {
            var newConfig = [];
            field[fieldLabelProp] = allValues[fieldLabelProp];
            field[fieldNameProp] = allValues[fieldNameProp]; // fields's common prop;

            field.requiredFlag = allValues.requiredFlag; // enabledFlag 是用来字段禁用的

            field.enabledFlag = allValues.enabledFlag;

            if (_isBoolean(allValues.labelDisplayFlag)) {
              var _newConfig$push;

              newConfig.push((_newConfig$push = {}, _defineProperty(_newConfig$push, attributeNameProp, 'labelDisplayFlag'), _defineProperty(_newConfig$push, attributeValueProp, allValues.labelDisplayFlag), _defineProperty(_newConfig$push, attributeTypeProp, DataType.Boolean), _newConfig$push));
            }

            if (_isString(allValues.description)) {
              var _newConfig$push2;

              newConfig.push((_newConfig$push2 = {}, _defineProperty(_newConfig$push2, attributeNameProp, 'description'), _defineProperty(_newConfig$push2, attributeValueProp, allValues.description), _defineProperty(_newConfig$push2, attributeTypeProp, DataType.String), _newConfig$push2));
            }

            if (_isString(allValues.placeholder)) {
              var _newConfig$push3;

              newConfig.push((_newConfig$push3 = {}, _defineProperty(_newConfig$push3, attributeNameProp, 'placeholder'), _defineProperty(_newConfig$push3, attributeValueProp, allValues.placeholder), _defineProperty(_newConfig$push3, attributeTypeProp, DataType.String), _newConfig$push3));
            }

            if (_isString(allValues.onChange)) {
              var _newConfig$push4;

              newConfig.push((_newConfig$push4 = {}, _defineProperty(_newConfig$push4, attributeNameProp, 'onChange'), _defineProperty(_newConfig$push4, attributeValueProp, allValues.onChange), _defineProperty(_newConfig$push4, attributeTypeProp, DataType.String), _newConfig$push4));
            }

            var FieldComponent = fieldComponents[field.componentType];

            if (FieldComponent) {
              FieldComponent.getConfigOfPropValues(allValues, newConfig);
            } else {
              var getConfigOfPropValuesFunc = "get".concat(field.componentType, "ConfigOfPropValues");

              if (_this2[getConfigOfPropValuesFunc]) {
                _this2[getConfigOfPropValuesFunc](allValues, newConfig);
              }
            }

            var prevFieldConfigs = field.config;
            field.config = newConfig.map(function (fieldConfig) {
              var prevFieldConfig = _find(prevFieldConfigs, function (prevC) {
                return prevC[attributeNameProp] === fieldConfig[attributeNameProp];
              });

              return _objectSpread(_objectSpread({}, prevFieldConfig), fieldConfig);
            });
            var newField = field; // { ...field };
            // 更新 feild 在 component 中的引用

            var fieldRefUpdate = false;

            _forEach(component.fields, function (fArr, rowIndex) {
              _forEach(fArr, function (f, colIndex) {
                if (f === field) {
                  fieldRefUpdate = true;
                  component.fields[rowIndex][colIndex] = newField;
                  return false;
                }
              });

              return !fieldRefUpdate;
            });

            if (_isFunction(onRefresh)) {
              onRefresh();
            }
          }
        });
      }
    }
    /**
     * deal component attribute change
     */

  }, {
    key: "handleComponentValuesChange",
    value: function handleComponentValuesChange(props, changeValues, allValues) {
      // 使用 allValues 重写 component 的属性
      // use allValues override component's props
      var _this$props3 = this.props,
          component = _this$props3.component,
          onRefresh = _this$props3.onRefresh;

      var _ref2 = this.editFormRef.current || {},
          validateFields = _ref2.validateFields;

      if (validateFields) {
        validateFields(function (err) {
          if (!err) {
            var newConfig = []; // common component's prop;

            component.templateCode = allValues.templateCode;
            component.description = allValues.description; // todo enabledFlag 没有了
            // component.enabledFlag = allValues.enabledFlag;
            // DynamicForm's attributes;
            // 查询URL

            if (_isString(allValues.queryUrl)) {
              var _newConfig$push5;

              newConfig.push((_newConfig$push5 = {}, _defineProperty(_newConfig$push5, attributeNameProp, 'queryUrl'), _defineProperty(_newConfig$push5, attributeValueProp, allValues.queryUrl), _defineProperty(_newConfig$push5, attributeTypeProp, DataType.String), _newConfig$push5));
            } // 提交URL


            if (_isString(allValues.submitUrl)) {
              var _newConfig$push6;

              newConfig.push((_newConfig$push6 = {}, _defineProperty(_newConfig$push6, attributeNameProp, 'submitUrl'), _defineProperty(_newConfig$push6, attributeValueProp, allValues.submitUrl), _defineProperty(_newConfig$push6, attributeTypeProp, DataType.String), _newConfig$push6));
            } // // 提交事件
            // if (isString(allValues.submit)) {
            //   newConfig.push({
            //     [attributeNameProp]: 'submit',
            //     [attributeValueProp]: allValues.submit,
            //     [attributeTypeProp]: DataType.String,
            //   });
            // }
            // 数据主键


            if (_isString(allValues.rowKey)) {
              var _newConfig$push7;

              newConfig.push((_newConfig$push7 = {}, _defineProperty(_newConfig$push7, attributeNameProp, 'rowKey'), _defineProperty(_newConfig$push7, attributeValueProp, allValues.rowKey), _defineProperty(_newConfig$push7, attributeTypeProp, DataType.String), _newConfig$push7));
            } // 是否可编辑


            if (_isBoolean(allValues.editable)) {
              var _newConfig$push8;

              newConfig.push((_newConfig$push8 = {}, _defineProperty(_newConfig$push8, attributeNameProp, 'editable'), _defineProperty(_newConfig$push8, attributeValueProp, allValues.editable), _defineProperty(_newConfig$push8, attributeTypeProp, DataType.Boolean), _newConfig$push8));
            }

            var prevComponentConfigs = component.config;
            component.config = newConfig.map(function (componentConfig) {
              var prevComponentConfig = _find(prevComponentConfigs, function (prevC) {
                return prevC[attributeNameProp] === componentConfig[attributeNameProp];
              });

              return _objectSpread(_objectSpread({}, prevComponentConfig), componentConfig);
            });

            if (_isFunction(onRefresh)) {
              onRefresh();
            }
          }
        });
      }
    }
  }]);

  return DynamicForm;
}(React.Component);

export default DynamicForm;